<template>
  <div :ref="`echarts-bar`" style="height: 230px" />
</template>

<script>
import * as echarts from 'echarts'
export default {
  name: 'EchartsOneBar',
  props: {
    proInfoCountByMonth: {
      type: Array,
      default: () => []
    },
    legend: {
      type: Array,
      default: () => ['项目个数']
    }
  },
  data() {
    return {
      echarts: null,
      proCountList: [], // 项目数量
      monthList: [] // 月份
    }
  },
  watch: {
    proInfoCountByMonth(values) {
      if (values && values.length) {
        this.initEcharts(values)
      }
    }
  },
  mounted() {
    this.initEcharts()
  },
  beforeDestroy() { // 清理工作 避免内存泄漏
    // 销毁监听事件
    window.removeEventListener('resize', this.resizeHandler)
    // 销毁 Echarts 实例
    this.echarts && this.echarts.dispose()
  },
  methods: {
    // 初始化图形
    initEcharts(values = []) {
      values.forEach(item => {
        this.proCountList.push(item.count)
        const month = +item.days.substr(-2, 2) + '月'
        this.monthList.push(month)
      })
      const options = {
        title: {
          text: '近一年巡检项目',
          left: '35%',
          top: '2%',
          textStyle: {
            color: '#01c4f7',
            fontWeight: 'bold',
            fontSize: 14

          }
        },
        tooltip: {
          // 提示框
          trigger: 'axis',
          axisPointer: {
            type: 'shadow'
          }
        },
        grid: {
          left: 50,
          right: 65,
          bottom: 45
        },
        xAxis: {
          type: 'category',
          data: this.monthList,
          name: '月份',
          nameTextStyle: {
            color: '#fff'
          },
          axisLabel: {
            color: '#fff',
            fontSize: 12
            // interval: 0
          },
          axisTick: {
            show: false
          },
          axisLine: {
            show: true,
            lineStyle: {
              color: '#fff',
              width: 1,
              type: 'solid'
            }
          }
        },
        yAxis: [
          {
            name: '(个)',
            min: 0,
            max: 5,
            minInterval: 1,
            type: 'value',
            nameTextStyle: {
              color: '#fff'
            },
            axisLine: {
              show: false
            },
            axisTick: {
              show: false
            },
            axisLabel: {
              color: '#eee',
              fontSize: 12
            },
            splitLine: {
              lineStyle: {
                color: ['#12fffe'],
                type: 'dotted'
              }
            }
          }
        ],
        series: [
          {
            name: this.legend[0],
            data: this.proCountList,
            type: 'bar',
            barWidth: '10px',
            itemStyle: {
              color: '#00ffe6',
              barBorderRadius: [180]
            },
            yAxisIndex: 0
          }
        ]
      }
      this.echarts = echarts.init(this.$refs[`echarts-bar`])
      this.echarts.setOption(options)
      // 绑定监听事件
      window.addEventListener('resize', this.resizeHandler)
      this.echarts.on('click', this.setMonth)
    },
    resizeHandler() {
      this.echarts.resize()
    },
    setMonth({ name }) {
      this.$emit('setMonth', name)
    }
  }
}
</script>

<style lang="scss" scoped>

</style>
